package com.hardware.service.impl.category;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hardware.common.CustomException;
import com.hardware.common.R;
import com.hardware.entity.Shop;
import com.hardware.entity.Supplier;
import com.hardware.entity.category.SupplierCategory;
import com.hardware.mapper.Category.SupplierCategoryMapper;
import com.hardware.service.ShopService;
import com.hardware.service.SupplierService;
import com.hardware.service.category.SupplierCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import static com.hardware.utils.RedisConstants.CACHE_CATEGORY_SUPPLIER_KEY;

@Service
public class SupplierCategoryServiceImpl extends ServiceImpl<SupplierCategoryMapper, SupplierCategory> implements SupplierCategoryService {
    @Autowired
    private SupplierService supplierCategory;
    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Supplier> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(Supplier::getCategoryId,id);
        int count = supplierCategory.count(dishLambdaQueryWrapper);

        //查询当前分类是否关联了商品，如果已经关联，抛出一个业务异常
        if(count > 0){
            //已经关联商品，抛出一个业务异常
            throw new CustomException("当前分类下关联了供应商，不能删除");
        }
        //正常删除分类
        super.removeById(id);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R<Page> page(int page, int pageSize){
        //分页构造器
        Page<SupplierCategory> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<SupplierCategory> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(SupplierCategory::getSort);
        //分页查询
        page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
}
